package com.github.wxiaoqi.security.biz.modules.pay.biz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.biz.request.PayOrderVerifyParam;
import com.github.wxiaoqi.security.biz.common.biz.request.VerifyRecordQueryRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.pay.entity.CmsPayOrder;
import com.github.wxiaoqi.security.biz.modules.pay.entity.CmsVerifyRecord;
import com.github.wxiaoqi.security.biz.modules.pay.mapper.CmsPayOrderMapper;
import com.github.wxiaoqi.security.biz.modules.pay.mapper.CmsVerifyRecordMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 核销记录
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-12-12 18:31:35
 */
@Service
public class CmsVerifyRecordBiz extends BaseBiz<CmsVerifyRecordMapper,CmsVerifyRecord> {
	
	private static final Logger log = LoggerFactory.getLogger(CmsVerifyRecordBiz.class);
	
	@Autowired
	private CmsPayOrderMapper payOrderMapper;
	
	
	/**
	 * 核销记录
	 * @param param
	 * @return
	 */
	public TableResultResponse<CmsVerifyRecord> verifyRecords(VerifyRecordQueryRequest param) {
		log.info("核销记录-请求参数：{}" + EntityUtils.beanToMap(param));
		Example example = new Example(CmsVerifyRecord.class);
		Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(param.getPlatformId())) {
			criteria.andEqualTo("platformId", param.getPlatformId());
		}
		if (!StringUtils.isEmpty(param.getMerchantId())) {
			criteria.andEqualTo("merchantId", param.getMerchantId());
		}
//		if (!StringUtils.isEmpty(param.getCustomerId())) {
//			criteria.andEqualTo("customerId", param.getCustomerId());
//		}
		example.setOrderByClause("create_time DESC");
		Page<CmsVerifyRecord> result = PageHelper.startPage(param.getPage(), param.getLimit());
		List<CmsVerifyRecord> list = mapper.selectByExample(example);
		return new TableResultResponse<CmsVerifyRecord>(result.getTotal(), list);
	}
	
	/**
	 * 核销
	 * 1.查询支付订单
	 * 2，修改支付订单状态
	 * 3，保存核销记录
	 * @param param
	 * @return
	 */
	public Map<String, Object> verifyOperation(PayOrderVerifyParam param) {
		log.info("核销订单，"+param);
		Map<String, Object> respMap = new HashMap<>();
		CmsPayOrder payOrderReq = new CmsPayOrder();
		payOrderReq.setPlatformId(param.getPlatformId());
		payOrderReq.setMerchantId(param.getMerchantId());
		payOrderReq.setPayOrderNo(param.getPayOrderNo());
		payOrderReq.setCustomerId(param.getCustomerId());
		payOrderReq.setVerifyStatus("00");
		payOrderReq.setTxStatus("00");
		CmsPayOrder selectParOrder = payOrderMapper.selectOne(payOrderReq);
		
		if (selectParOrder == null) {
			log.info("核销订单，支付订单不存在");
			respMap.put("code", BizResponseCode.ORDER_NO_EXIST.getCode());
			respMap.put("message", BizResponseCode.ORDER_NO_EXIST.getMessage());
			return respMap;
		}
		try {
			selectParOrder.setVerifyStatus("01");
			payOrderMapper.updateByPrimaryKeySelective(selectParOrder);
			
			CmsVerifyRecord record = new CmsVerifyRecord();
			record.setCreateTime(new Date());
			record.setGoodsName(param.getGoodsName());
			record.setMerchantId(param.getMerchantId());
			record.setOrderNo(String.valueOf(SnowFlake.getId()));
			record.setPlatformId(param.getPlatformId());
			record.setVerifyStatus("00");
			record.setPayOrderNo(param.getPayOrderNo());
			mapper.insert(record);
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			log.info("核销订单异常，"+e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return respMap;
		}
		log.info("核销订单，结果 "+respMap);
		return respMap;
	}
	
}